Passed
Push — develop ( 77c94a...be0d3a )
by Xaver
02:54
created

node.js ➔ ... ➔ nodeLink   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
c 0
b 0
f 0
nc 2
dl 0
loc 12
rs 9.95
nop 1

1 Function

Rating   Name   Duplication   Size   Complexity  
A node.js ➔ ... ➔ V.h(ꞌaꞌ).on.click 0 3 1
1
define(['sorttable', 'snabbdom', 'd3-interpolate', 'helper', 'utils/node'],
2
  function (SortTable, V, d3Interpolate, helper, nodef) {
3
    'use strict';
4
    V = V.default;
5
6
    function showStatImg(o, d) {
7
      var subst = {
8
        '{NODE_ID}': d.node_id,
9
        '{NODE_NAME}': d.hostname.replace(/[^a-z0-9\-]/ig, '_'),
10
        '{TIME}': d.lastseen.format('DDMMYYYYHmmss'),
11
        '{LOCALE}': _.locale()
12
      };
13
      return helper.showStat(V, o, subst);
14
    }
15
16
    return function (el, d, linkScale, nodeDict) {
17
      function nodeLink(node) {
18
        return V.h('a', {
19
          props: {
20
            className: node.is_online ? 'online' : 'offline',
21
            href: router.generateLink({ node: node.node_id })
22
          }, on: {
23
            click: function (e) {
24
              router.fullUrl({ node: node.node_id }, e);
25
            }
26
          }
27
        }, node.hostname);
28
      }
29
30
      function nodeIdLink(nodeId) {
31
        if (nodeDict[nodeId]) {
32
          return nodeLink(nodeDict[nodeId]);
33
        }
34
        return nodeId;
35
      }
36
37
      function showGateway(node) {
38
        var gatewayCols = [
39
          V.h('span', [
40
            nodeIdLink(node.gateway_nexthop),
41
            V.h('br'),
42
            _.t('node.nexthop')
43
          ]),
44
          V.h('span', { props: { className: 'ion-arrow-right-c' } }),
45
          V.h('span', [
46
            nodeIdLink(node.gateway),
47
            V.h('br'),
48
            'IPv4'
49
          ])
50
        ];
51
52
        if (node.gateway6 !== undefined) {
53
          gatewayCols.push(V.h('span', [
54
            nodeIdLink(node.gateway6),
55
            V.h('br'),
56
            'IPv6'
57
          ]));
58
        }
59
60
        return V.h('td', { props: { className: 'gateway' } }, gatewayCols);
61
      }
62
63
      function renderNeighbourRow(n) {
64
        var icons = [V.h('span', { props: { className: 'icon ion-' + (n.link.type.indexOf('wifi') === 0 ? 'wifi' : 'share-alt'), title: _.t(n.link.type) } })];
65
        if (helper.hasLocation(n.node)) {
66
          icons.push(V.h('span', { props: { className: 'ion-location', title: _.t('location.location') } }));
67
        }
68
69
        return V.h('tr', [
70
          V.h('td', icons),
71
          V.h('td', nodeLink(n.node)),
72
          V.h('td', n.node.clients),
73
          V.h('td', { style: { color: linkScale((n.link.source_tq + n.link.target_tq) / 2) } }, helper.showTq(n.link.source_tq) + ' - ' + helper.showTq(n.link.target_tq)),
74
          V.h('td', helper.showDistance(n.link))
75
        ]);
76
      }
77
78
      var self = this;
79
      var header = document.createElement('h2');
80
      var table = document.createElement('table');
81
      var images = document.createElement('div');
82
      var neighbours = document.createElement('h3');
83
      var headings = [{
84
        name: '',
85
        sort: function (a, b) {
86
          return a.link.type.localeCompare(b.link.type);
87
        }
88
      }, {
89
        name: 'node.nodes',
90
        sort: function (a, b) {
91
          return a.node.hostname.localeCompare(b.node.hostname);
92
        },
93
        reverse: false
94
      }, {
95
        name: 'node.clients',
96
        class: 'ion-people',
97
        sort: function (a, b) {
98
          return a.node.clients - b.node.clients;
99
        },
100
        reverse: true
101
      }, {
102
        name: 'node.tq',
103
        class: 'ion-connection-bars',
104
        sort: function (a, b) {
105
          return a.link.source_tq - b.link.source_tq;
106
        },
107
        reverse: true
108
      }, {
109
        name: 'node.distance',
110
        class: 'ion-arrow-resize',
111
        sort: function (a, b) {
112
          return (a.link.distance === undefined ? -1 : a.link.distance) -
113
            (b.link.distance === undefined ? -1 : b.link.distance);
114
        },
115
        reverse: true
116
      }];
117
      var tableNeighbour = new SortTable(headings, 1, renderNeighbourRow);
118
119
      el.appendChild(header);
120
      el.appendChild(table);
121
      el.appendChild(neighbours);
122
      el.appendChild(tableNeighbour.el);
123
      el.appendChild(images);
124
125
      self.render = function render() {
126
        V.patch(header, V.h('h2', d.hostname));
127
128
        var children = [];
129
130
        config.nodeAttr.forEach(function (row) {
131
          var field = d[row.value];
132
          if (typeof row.value === 'function') {
133
            field = row.value(d, nodeDict);
134
          } else if (nodef['show' + row.value] !== undefined) {
135
            field = nodef['show' + row.value](d);
136
          }
137
138
          if (field) {
139
            if (typeof field !== 'object') {
140
              field = V.h('td', field);
141
            }
142
            children.push(V.h('tr', [
143
              row.name !== undefined ? V.h('th', _.t(row.name)) : null,
144
              field
145
            ]));
146
          }
147
        });
148
149
        children.push(V.h('tr', [
150
          V.h('th', _.t('node.gateway')),
151
          showGateway(d)
152
        ]));
153
154
        var elNew = V.h('table', children);
155
        table = V.patch(table, elNew);
156
        table.elm.classList.add('attributes');
0 ignored issues
show
Bug introduced by
The variable table is changed as part of the for loop for example by V.patch(table, elNew) on line 155. Only the value of the last iteration will be visible in this function if it is called after the loop.
Loading history...
157
158
        V.patch(neighbours, V.h('h3', _.t('node.link', d.neighbours.length) + ' (' + d.neighbours.length + ')'));
159
        if (d.neighbours.length > 0) {
160
          tableNeighbour.setData(d.neighbours);
161
          tableNeighbour.el.elm.classList.add('node-links');
162
        }
163
164
        if (config.nodeInfos) {
165
          var img = [];
166
          config.nodeInfos.forEach(function (nodeInfo) {
167
            img.push(V.h('h4', nodeInfo.name));
168
            img.push(showStatImg(nodeInfo, d));
169
          });
170
          images = V.patch(images, V.h('div', img));
171
        }
172
      };
173
174
      self.setData = function setData(data) {
175
        if (data.nodeDict[d.node_id]) {
176
          d = data.nodeDict[d.node_id];
177
        }
178
        self.render();
179
      };
180
      return self;
181
    };
182
  });
183